package com.LeetCode.why65;

/**
 * Created by Aqc on 2017/6/19.
 */
public class ValidNumber {
    public static boolean isNumber(String s) {
        boolean dout = false;//小数点只能有一个
        boolean fuhao = false;//正负号
        boolean fuhaoE = false;//e指数的正负号
        boolean numberE = false;//e最多只能有一个
        boolean numberN = false;//是e前的部分，true表示这一部分结束

        s = s.trim();//去掉两端空格
        char[] trans = s.toCharArray();
        int len = trans.length;
        if (len == 0) {
            return false;
        }
        if (trans[0] == '+' || trans[0] == '-') {
            fuhao = true;
        }
        for (int i = (fuhao == true ? 1 : 0); i < len; i++) {
            char temp = trans[i];
            if ((temp >= '0' && temp <= '9')) {
                if (numberE == true) {
                    fuhaoE = true;
                }
                continue;
            }
            if (temp == '.') {
                if (dout == true || numberE == true) {
                    return false;//多个逗号返回
                } else {
                    dout = true;
                    if (i == len - 1 || trans[i + 1] == 'e')
                        return false;//逗号后面必须有数字,逗号和e不能连在一起
                    continue;
                }
            }
            if (temp == 'e') {
                if (numberE == true) {
                    return false;//多个e返回
                } else {
                    numberE = true;
                    if (i == len - 1)
                        return false;//e后面必须有数字
                    continue;
                }
            }
            if (temp == '+' || temp == '-') {
                if (numberE == true) {
                    if (fuhaoE == true) {
                        return false;//多符号返回
                    } else {
                        fuhaoE = true;
                        if (i == len - 1)
                            return false;//e后面必须有数字
                        continue;
                    }
                } else return false;//多符号返回
            }
            //System.out.println(trans[i]);
            return false;
        }
        return true;

    }


    public static void main(String[] args) {//题目没有解答出来，因为题目很sb
        String s = "-.";//s = " "//s = "e"//s = "."//s = ".3"//s = "1e."//s = "4e+"
        boolean a = isNumber(s);
        System.out.println(a == true ? "是" : "否");
    }
}
